package com.esgpro.esg_api.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class JSONResult {
    private static final JSONObject result = new JSONObject();

    private static JSONObject success() {
        result.put("code", 200);
        result.put("message", "success");
        return result;
    }

    private static JSONObject error() {
        result.put("code", 404);
        result.put("message", "error");
        return result;
    }

    public JSONObject getResult(boolean flag) {
        result.fluentClear();
        if (flag) {
            return success();
        }
        return error();
    }

    public JSONObject putMapInResult(String key, Object value) {
        result.fluentClear();
        if (!key.equals("")) {
            JSONObject success = success();
            success.put(key, value);
            return success;
        }
        return error();
    }

    public JSONObject getDataResult(Object value) {
        result.fluentClear();
        if (!value.equals("")) {
            JSONObject success = success();
            success.put("data", value);
            return success;
        }
        return error();
    }

}
